package edu.zjut.sjq.service;

import edu.zjut.sjq.bean.Comment;
import edu.zjut.sjq.bean.Result;
import edu.zjut.sjq.bean.User;
import edu.zjut.sjq.mapper.CommentMapper;
import edu.zjut.sjq.mapper.GradeMapper;
import edu.zjut.sjq.mapper.LikeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by sue on 2017/3/18.
 */
@Service
public class CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private HttpSession session;

    // 添加评论
    public Result addComment(Comment comment){
        Result result = new Result();
        result.setRes(0);
        User user = (User) session.getAttribute("user");
        if (user==null){
            result.setMsg("您好，请先登录！");
            return result;
        }
        comment.setUserId(user.getId());
        comment.setCreateTime(new Date());
        int res = commentMapper.insert(comment);
        result.setRes(res);
        if (res==1)
            result.setMsg("评论成功");
        else
            result.setMsg("保存成功");

        return result;
    }

    // 获取评论
    public List<Comment> getComment(Integer articleId,Integer topicId){
        return commentMapper.selectByRecordId(articleId,topicId);
    }

    // 删除评论
    public Integer deleteComment(Integer commentId){
        return commentMapper.deleteByPrimaryKey(commentId);
    }

}
